package com.appcenter.application.mapper;

import com.appcenter.application.dto.externalapi.BusinessSystemUserDto;
import com.appcenter.application.dto.roles.RoleDto;
import com.appcenter.application.dto.users.UserApiDto;
import com.appcenter.application.dto.users.UserDto;
import com.appcenter.application.input.jobUser.GetJobUserInput;
import com.appcenter.application.input.user.*;
import com.appcenter.application.input.userpicker.SearchUserPickerListInput;
import com.appcenter.application.model.Orgs;
import com.appcenter.application.model.Users;
import com.appcenter.application.output.externalapi.GetUsersDetaileOutput;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

@Repository
@org.apache.ibatis.annotations.Mapper
public interface UsersMapper extends Mapper<Users> {

    @Select("select * from users where usercode=#{UserCode} and versionno=#{versionNo}")
    Users getUsersByUserCode(@Param("UserCode") String UserCode, @Param("versionNo") Long versionNo);

    @Select("select * from users where usercode=#{UserCode} and Id <> #{Id} and versionno=#{versionNo}")
    Users getUsersByUserCodeNoId(@Param("UserCode") String UserCode, @Param("Id") String Id, @Param("versionNo") Long versionNo);

    @Select("select * from users where id = #{Id} and versionno=#{versionNo}")
    Users getUserById(@Param("Id") String Id, @Param("versionNo") Long versionNo);

    @Select("select * from users where UserCode = #{UserCode} and versionno=#{versionNo}")
    Users getUserByUserCode(@Param("UserCode") String UserCode, @Param("versionNo") String versionNo);

    @Results({
            @Result(column = "PID", property = "userAddress", one = @One(select = "com.appcenter.application.mapper.UserAddrsMapper.getUserAddsByUserId"))
    })
    @Select("<script>" +
            "select *,Id as PID from users where 1=1 and versionno=#{versionNo}" +
            "" +
            " <when test='keyword!=null'>" +
            " AND (UserCode like concat('%',#{keyword},'%') or UserName like concat('%',#{keyword},'%') or DingUserId like concat('%',#{keyword},'%'))" +
            " </when>" +
            "" +
            "</script>")
    List<Users> searchUsers(GetDataUsersInput input);

    @Select("select * from  users where versionno=#{versionNo} and (UserCode=#{userCode} or Id=#{id})")
    Users getUser(GetUserInput input);

    @Select("<script>" +
            " select users.* from users INNER JOIN  orgusers ON users.Id=orgusers.UserId" +
            " INNER JOIN orgs on  orgusers.OrgId=orgs.Id" +
            "" +
            " <when test='IsOneLevel'>" +
            " where orgs.OrgCode=#{OrgCode} " +
            " </when>" +
            "" +
            " <when test='!IsOneLevel'>" +
            "  where orgusers.OrgCodePath LIKE concat('%',#{OrgCodePath},'%') " +
            " </when>" +
            "" +
            " <when test='AppCode!=null'>" +
            " AND orgs.AppCode=#{AppCode}" +
            " </when>" +
            "" +
            " <when test='AppCode==null'>" +
            " AND orgs.AppCode IS NULL" +
            " </when>" +
            "" +
            " AND users.AccountIsDisabled  IS NULL OR  users.AccountIsDisabled!=1" +
            " and users.VersionNo=#{VersionNo} and orgs.VersionNo=#{VersionNo} and orgusers.VersionNo=#{VersionNo}" +
            "</script>")
    List<Users> getsUsersByOrgCode(@Param("AppCode") String AppCode, @Param("OrgCode") String OrgCode, @Param("IsOneLevel") boolean IsOneLevel, @Param("OrgCodePath") String OrgCodePath, @Param("VersionNo") Long VersionNo);


    @Select("<script>" +
            "SELECT users.* from  users INNER JOIN roleusers ON users.Id=roleusers.userId" +
            " INNER JOIN roles ON roles.Id=roleusers.RoleId  " +
            " INNER JOIN apps ON roles.AppId=apps.Id " +
            "" +
            " <when test='appCode!=null'>" +
            " WHERE apps.AppCode=#{appCode}" +
            " </when>" +
            "" +
            " <when test='appCode==null'>" +
            " WHERE apps.AppCode is #{appCode}" +
            " </when>" +
            "" +
            " AND  roles.RoleCode=#{roleCode}" +
            " AND users.AccountIsDisabled  IS NULL OR  users.AccountIsDisabled!=1" +
            " and users.VersionNo=#{versionNo}" +
            "</script>")
    List<Users> getsUsersByRoleCode(GetUsersInput input);


    @Select("<script>" +
            "select u.*,o.jobName,r.orgName,concat(r.OrgNamePath,'/',o.JobName) as fullNamePath,concat(r.OrgIdPath,'/',o.Id) as fullIdPath from users u join jobusers j on j.UserId=u.Id  and (u.AccountIsDisabled=0 or u.AccountIsDisabled is NULL) AND u.VersionNo=#{versionNo} and j.VersionNo=#{versionNo} "+

            "left join orgjobs oj on oj.id=j.orgJobId and oj.VersionNo=#{versionNo} "+

            "left join jobs o on o.Id=oj.JobId and o.ValidStatus=1 and o.Deleted=0 and o.VersionNo=#{versionNo} "+
            "left join jobcategorys b on b.Id=o.JobCategoryId and b.ValidStatus=1 and b.Deleted=0 "+

            "left join orgs r on r.id=oj.OrgId and r.ValidStatus=1 and r.VersionNo=#{versionNo} "+
            "left join apps a on r.AppCode  =a.AppCode and a.ValidStatus=1 "+
            "where a.AppCode=#{appCode} " +
            "" +
            " <when test='jobId!=null'>" +
            " and o.Id=#{jobId}" +
            " </when>" +
            ""+
            "" +
            " <when test='orgId!=null'>" +
            " and  r.OrgIdPath like concat((select OrgIdPath from orgs where id=#{orgId} and VersionNo=#{versionNo}),'%') " +
            " </when>" +
            ""+
            " <when test='keyword!=null'>" +
            " and (u.UserCode like concat('%',#{keyword},'%') or u.UserName like concat('%',#{keyword},'%') or u.Phone like concat('%',#{keyword},'%'))" +
            " </when>" +
            ""+
            "</script>")
    List<GetUsersDetaileOutput.User> getsUsersByJobId(GetJobUserInput input);






    @Update("update users set Password=#{newPassword} where id=#{id} and versionNo=#{versionNo}")
    int ChangePassword(@Param("id") String id, @Param("newPassword") String newPassword,@Param("versionNo") Long versionNo);






    @Select("<script>" +
            "select * from users where 1=1 and VersionNo=#{versionNo}" +
            "" +
            " <when test='keyword!=null'>" +
            " AND (UserCode like concat('%',#{keyword},'%') or UserName like concat('%',#{keyword},'%') or DingUserId like concat('%',#{keyword},'%') )" +
            " </when>" +
            "" +
            " order by UserCode" +
            "</script>")
    public List<Users> searchUsersList(SearchUsersInput input);

    @Update("update users set AccountIsDisabled=#{AccountIsDisabled} where id=#{Id} and versionNo=#{versionNo}")
    int changeAccountStatusById(ChangeAccountStatusInput input);

    @Update("update users set AccountIsDisabled=#{AccountIsDisabled} where UserCode=#{UserCode} and versionNo=#{versionNo}")
    int changeAccountStatusByUserCode(ChangeAccountStatusInput input);

    @Select("<script>" +
            " SELECT users.* from  users INNER JOIN orgusers ON users.Id=orgusers.UserId where 1=1 and users.VersionNo=#{versionNo} and orgusers.VersionNo=#{versionNo}" +
            "" +
            " <when test='AppCode!=null'>" +
            " and orgusers.AppCode=#{AppCode}" +
            " </when>" +
            "" +
            " <when test='AppCode==null'>" +
            " and orgusers.AppCode is #{AppCode}" +
            " </when>" +
            "" +
            " <when test='keyword!=null'>" +
            " and (users.UserCode like concat('%',#{keyword},'%') or users.UserName like concat('%',#{keyword},'%'))" +
            " </when>" +
            "" +
            " and orgusers.ValidStatus=1 " +
            "" +
            " and (users.AccountIsDisabled=0 or users.AccountIsDisabled is null ) " +
            " order by users.UserCode" +
            "" +
            "</script>")
    List<UserDto> SearchUsers(SearchUserPickerListInput input);

    @Select("<script>" +
            " select users.* from  roleusers join  users on roleusers.UserId=users.Id where 1=1" +
            " and roleusers.ValidStatus=1 and roleusers.RoleId=#{RoleId} and (users.AccountIsDisabled=0 or users.AccountIsDisabled is null )" +
            " and users.VersionNo=#{versionNo}" +
            " order by users.UserCode" +
            "" +
            "</script>")
    List<UserDto> SearchRoleUsers(SearchUserPickerListInput input);

    @Select("<script>" +
            " select users.* from  orgusers join  users on orgusers.UserId=users.Id where 1=1" +
            " and orgusers.ValidStatus=1 and orgusers.OrgId=#{OrgId} and (users.AccountIsDisabled=0 or users.AccountIsDisabled is null )" +
            " and users.VersionNo=#{versionNo} and orgusers.VersionNo=#{versionNo}" +
            " order by users.UserCode" +
            "" +
            "</script>")
    List<UserDto> SearchOrgUsers(SearchUserPickerListInput input);

    @Select("select roles.* from roles join apps on apps.Id=roles.AppId and roles.ValidStatus=1 and apps.AppCode=#{AppCode}")
    List<RoleDto> GetRolesByAppCpde(String AppCode);


    @Results({
            @Result(column = "PID", property = "userAddress",
                    one = @One(select = "com.appcenter.application.mapper.UserAddrsMapper.getUserAddsByUserId")),

            @Result(column = "{appCode=appCode,userId=userId,versionNo=versionNo}", property = "userOrgs", javaType = List.class, many = @Many
                    (select = "com.appcenter.application.mapper.OrgUsersMapper.getOrgsByUserId")),

           @Result(column = "{appCode=appCode,userId=userId,versionNo=versionNo}", property = "userRoles", javaType = List.class, many = @Many
                    (select = "com.appcenter.application.mapper.RoleUsersMapper.getRolesByUserId")),

            @Result(column = "{appCode=appCode,userId=userId,versionNo=versionNo}", property = "userJobs", javaType = List.class, many = @Many
                    (select = "com.appcenter.application.mapper.JobUsersMapper.getJobsByUserId")),

    }
    )
    @SelectProvider(type = OrgUsersProvider.class, method = "GetUserApi")
    List<UserApiDto> GetUsersAPI(@Param("appCode") String appCode, @Param("orgCode") String orgCode, @Param("roleCode") String roleCode, @Param("jobCode") String jobCode, @Param("userCode") String userCode,@Param("versionNo") Long versionNo);


    @Results({
            @Result(column = "{appCode=appCode,userId=userId,versionNo=versionNo}", property = "orgs", javaType = List.class, many = @Many
                    (select = "com.appcenter.application.mapper.OrgUsersMapper.getBusinessSystemOrg")),
            @Result(column = "{appCode=appCode,userId=userId,versionNo=versionNo}", property = "jobs", javaType = List.class, many = @Many
                    (select = "com.appcenter.application.mapper.JobUsersMapper.getBusinessSystemJob")),

    }
    )
    @SelectProvider(type = OrgUsersProvider.class, method = "GetUserApi")
    List<BusinessSystemUserDto> GetBusinessSystemUsersAPI(@Param("appCode") String appCode, @Param("orgCode") String orgCode, @Param("roleCode") String roleCode, @Param("jobCode") String jobCode, @Param("userCode") String userCode, @Param("versionNo") Long versionNo);




}
